package com.echoesnet.echoesshortvideo.Activities;

import android.app.Activity;
import android.arch.lifecycle.Observer;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.AlphaAnimation;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.echoesnet.echoesshortvideo.R;
import com.echoesnet.echoesshortvideo.Utils.CommonUtils;
import com.echoesnet.echoesshortvideo.Utils.ToastUtils;
import com.echoesnet.echoesshortvideo.ViewModels.LoginViewModel;
import com.echoesnet.echoesshortvideo.ViewModels.StepRegisterViewModel;
import com.echoesnet.echoesshortvideo.views.dialog.CustomAlertDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class NewBindPhoneAct extends BaseActivity
{
    private static final String TAG=NewBindPhoneAct.class.getSimpleName();

    @BindView(R.id.phone)
    EditText editText;
    @BindView(R.id.nextOk)
    ImageView ivNext;
    @BindView(R.id.clear)
    ImageView clear;
    @BindView(R.id.scrollLayout)
    RelativeLayout rl;
    private LoginViewModel loginModel;
    //屏幕高度
    private int screenHeight = 0;
    //软件盘弹起后所占高度阀值
    private int keyHeight = 0;
    //Activity最外层的Layout视图
    @BindView(R.id.root_layout)
    View activityRootView;

    private Activity mAct;
    private String phone;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_new_bind_phone);
        ButterKnife.bind(this);
        mAct=this;

        loginModel = new LoginViewModel();
        loginModel.getLoginLive().observe(this, new Observer<String>()
        {
            @Override
            public void onChanged(@Nullable String type)
            {
                if (type == null)
                    return;

                if(type.equals("1"))
                {
                    Intent intent = new Intent(NewBindPhoneAct.this,NewBindCodeAct.class);
                    intent.putExtra("phone", phone);
                    startActivity(intent);
                }
                else
                    new CustomAlertDialog(mAct)
                            .builder()
                            .setMsg("该手机号已经绑定了其他账号\n试试其他的手机号吧!")
                            .setPositiveButton("确认", v ->
                            {
                            })
                            .show();
            }
        });

        editText.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after)
            {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                //随字数改变按钮状态
                changeChannel(s.length());
                if (s.length() < 11)
                {
                    ivNext.setEnabled(false);
                    ivNext.setAlpha(0.5f);
                } else
                {
                    ivNext.setEnabled(true);
                    ivNext.setAlpha(1f);
                }
            }

            @Override
            public void afterTextChanged(Editable s)
            {

            }
        });

        //获取屏幕高度
        screenHeight = CommonUtils.getScreenHeight(this);
        //阀值设置为屏幕高度的1/3
        keyHeight = screenHeight/3;
        //添加layout大小发生改变监听器
        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);

        ivNext.setEnabled(false);
        ivNext.setAlpha(0.5f);

    }

    @Override
    protected void onResume()
    {
        super.onResume();
    }

    @OnClick({R.id.cancle, R.id.nextOk, R.id.clear})
    void onViewClick(View view)
    {
        switch (view.getId())
        {
            case R.id.cancle:
                finish();
                break;
            case R.id.clear:
                editText.setText("");
                break;
            case R.id.nextOk:
                //验证手机号
                if (!CommonUtils.verifyInput(3, editText.getText().toString()))
                {
                    //--------------测试时暂时不做限制  上线要拦住！！！
                    ToastUtils.showShort("请输入正确的手机号");
                    return;
                }

                phone=editText.getText().toString().trim();

                //下一步
                if (loginModel != null)
                    loginModel.checkUserRegisterState(phone);
                break;
        }
    }

    private void changeChannel(int count)
    {
        if (count == 0 )
        {
            clear.setVisibility(View.INVISIBLE);
        } else if (count != 0 )
        {
            clear.setVisibility(View.VISIBLE);
        }
    }

    ViewTreeObserver.OnGlobalLayoutListener layoutListener = new ViewTreeObserver.OnGlobalLayoutListener()
    {

        @Override
        public void onGlobalLayout()
        {
            activityRootView.getViewTreeObserver().removeOnGlobalLayoutListener(layoutListener);

            Rect r = new Rect();
            activityRootView.getWindowVisibleDisplayFrame(r);

            //现在认为只要控件将Activity向上推的高度超过了1/3屏幕高，就认为软键盘弹起
            if (screenHeight-r.bottom  > keyHeight)
            {
                updateViewHeight(true);
            } else
            {
                updateViewHeight(false);
            }
            activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
        }
    };

    private void updateViewHeight(boolean flag)
    {
        RelativeLayout.LayoutParams pa= (RelativeLayout.LayoutParams) rl.getLayoutParams();
        pa.topMargin=flag?LoginAct.TOP_MARGIN:0;
        rl.setLayoutParams(pa);
    }
}